package bank

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
)

func flowData(c *gin.Context) {
	sql.HttpDataById(c, &model.BankFlow{})
}

/**
 *创建银行流水，修改账户金额
 */
func flowAdd(c *gin.Context) {
	sql.HttpHandle(sql.ADD, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var bean model.BankFlow
		if err := c.Bind(&bean); err != nil {
			fmt.Println(err)
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		fmt.Println("金额：", bean.Money)

		tx := db.Begin()

		// 1、创建银行流水
		if err := tx.Create(&bean).Error; err != nil {
			tx.Callback()
			tx.Error = errors.New(sql.ErrorService)
			return tx, nil
		}

		// 2、修改银行账户金额
		isAdd := bean.Type == "收入"
		if err := balanceChanges(tx, bean.AccountId, bean.Money, isAdd); err != nil {
			return tx, nil
		}
		tx.Commit()
		return db, bean
	})
}

func flowUpdates(c *gin.Context) {
	sql.HttpHandle(sql.ADD, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var bean model.BankFlow
		if err := c.Bind(&bean); err != nil {
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		tx := db.Begin()

		// 1、根据流水ID查询要修改的数据
		var oldFlow model.BankFlow
		if err := tx.Where("id = ?", bean.ID).First(&oldFlow).Error; err != nil {
			tx.Callback()
			tx.Error = errors.New(sql.ErrorService)
			return tx, nil
		}

		//2、根据老的银行流水还原账户余额
		if err := balanceChanges(tx, oldFlow.AccountId, oldFlow.Money, !(bean.Type == "收入")); err != nil {
			return tx, nil
		}

		//3、修改的银行流水
		if err := tx.Updates(&bean).Error; err != nil {
			tx.Callback()
			tx.Error = errors.New(sql.ErrorService)
			return tx, nil
		}

		//4、根据银行流水修改账户余额
		if err := balanceChanges(tx, bean.AccountId, bean.Money, bean.Type == "收入"); err != nil {
			return tx, nil
		}

		tx.Commit()
		return db, bean
	})
}

func flowDelete(c *gin.Context) {
	sql.HttpHandle(sql.ADD, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		idStr := c.Query("id")
		id, strError := strconv.Atoi(idStr)
		if strError != nil {
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		tx := db.Begin()
		var bankFlow model.BankFlow
		// 1、查询要删除的银行流水
		if err := tx.Where("id = ?", id).First(&bankFlow).Error; err != nil {
			tx.Callback()
			tx.Error = errors.New(sql.ErrorService)
			return tx, nil
		}

		//2、删除银行流水
		if err := tx.Delete(&bankFlow).Error; err != nil {
			tx.Callback()
			tx.Error = errors.New(sql.ErrorService)
			return tx, nil
		}

		//3、根据银行流水修改账户余额
		if err := balanceChanges(tx, bankFlow.AccountId, bankFlow.Money, !(bankFlow.Type == "收入")); err != nil {
			return tx, nil
		}

		tx.Commit()
		return db, nil
	})
}

func flowList(c *gin.Context) {
	sql.HttpList(c, &[]model.BankFlow{}, func(db *gorm.DB) *gorm.DB {

		if accountId := c.Query("accountId"); accountId != "" {
			db = db.Where("account_id = ?", accountId)
		}

		if start := c.Query("startTime"); start != "" {
			db = db.Where("date >= ? ", start+" 00:00:00")
		}

		if end := c.Query("endTime"); end != "" {
			db = db.Where("date <= ? ", end+" 23:59:59")
		}

		if action := c.Query("action"); action == "selector" {
			db = db.Where("balance <> ?", 0)
		}

		if attribute := c.Query("type"); attribute != "" {
			db.Where("type = ?", attribute)
		}
		if status := c.Query("status"); status != "" {
			db.Where("status = ?", status)
		}
		if accountId := c.Query("accountId"); accountId != "" {
			db.Where("accountId = ?", accountId)
		}

		db = db.Order("date DESC,time DESC")
		return db
	})
}
